package com.wondersgroup.fjzlpt.yyzyzx.service.impl.yyzygl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.wondersgroup.fjzlpt.yyzyzx.dao.inter.yyzygl.yysz.JgkhgzDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wondersgroup.fjzlpt.yyzyzx.dao.inter.yyzygl.yysz.HygzDao;
import com.wondersgroup.fjzlpt.yyzyzx.service.inter.yyzygl.HygzglService;

/**
 * 号源规则管理
 * @author gzb   
 * 2017年8月10日
 */
@Service
public class HygzglServiceImpl implements HygzglService{

	@Autowired
	private HygzDao hygzDao;
	@Autowired
	private JgkhgzDao jgkhgzDao;
	
	
	@Override
	public JSONObject save(Map map) {
		JSONObject result = new JSONObject();

		
		String type = map.get("type").toString();
		if("add".equals(type)){
			insert(map, result);
		}else{
			update(map, result);
		}
		return result;
	}
	
	/**
	 * 添加号源规则
	 * @param map
	 * @param result
	 */
	private void insert(Map map, JSONObject result){
		try {
			Date nowDate = new Date();
			map.put("GZBH", UUID.randomUUID().toString());
			map.put("CJSJ", nowDate);
			map.put("GXSJ", nowDate);
			map.put("CJYH", map.get("GXYH"));
			
			hygzDao.insert(map);
			result.put("state", "1");
			result.put("info", "号源规则保存成功！");
		} catch (Exception e) {
			result.put("info", "号源规则保存失败！");
			throw new RuntimeException("号源规则保存失败！");
		}
	}
	
	/**
	 * 修改号源规则
	 * @param map
	 * @param result
	 */
	private void update(Map map, JSONObject result){
		try {
			map.put("GXSJ", new Date());
			
			hygzDao.updateForPrimaryKey(map);
			result.put("state", "1");
			result.put("info", "号源规则修改成功！");
		} catch (Exception e) {
			result.put("info", "号源规则修改失败！");
			throw new RuntimeException("号源规则修改失败！");
		}
	}
	
	/**
	 * 分页查询号源规则
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject queryPage(Map map) {
		return hygzDao.queryPage(map);
	}

	/**
	 * 删除号源规则
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject delete(Map map) {
		JSONObject result = new JSONObject();
		try {
			hygzDao.deleteForPrimaryKey(map);
			result.put("state", "1");
			result.put("info", "删除成功！");
		} catch (Exception e) {
			result.put("info", "号源规则删除失败，请稍后重试!");
			throw new RuntimeException("号源规则删除失败！");
		}
		return result;
	}

	/**
	 * 保存 号源规则的关联机构
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject saveGljg(Map map) {
		JSONObject result = new JSONObject();
		
		try {
			Object gljg = map.get("gljg");
			if(gljg!=null && !"".equals(gljg)){
				//删除原关联机构
				jgkhgzDao.deleteByGzbh(map.get("GZBH").toString());
				//新增关联机构
				JSONArray gljgArr = JSONArray.parseArray(gljg.toString());
				List<Map> list = new ArrayList<Map>();
				for(int i=0;i<gljgArr.size();i++){
					Map itemMap = (Map)gljgArr.getJSONObject(i);
					itemMap.put("GZBH", map.get("GZBH"));
					list.add(itemMap);
				}
				jgkhgzDao.insertBatch(list);
			}
			
			result.put("state", "1");
			result.put("info", "关联机构保存成功！");
		} catch (Exception e) {
			result.put("info", "关联机构保存失败,请稍后再试！");
			throw new RuntimeException("关联机构保存失败！");
		}
		return result;
	}

	/**
	 * 查询 号源规则所有的关联机构
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject queryGljg(Map map) {
		JSONObject result = new JSONObject();
		try {
			JSONArray gljgArr = jgkhgzDao.queryAll(map);
			result.put("rows", gljgArr);
			result.put("state", "1");
		} catch (Exception e) {
			result.put("info", "查询关联机构失败！");
			throw new RuntimeException("查询关联机构失败！");
		}
		return result;
	}
}
